package com.wbs.shop.nuby.utils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

public class StringUtils {
    public static String defaultIfEmpty(Object str) {
        if (str == null) {
            return "";
        }
        return isEmpty(str.toString()) ? "" : str.toString();
    }

    public static String defaultIfEmpty(Object str, String defaultStr) {
        if (str == null) {
            return defaultStr;
        }
        return isEmpty(str.toString()) ? defaultStr : str.toString();
    }

    public static boolean isEmpty(CharSequence cs) {
        return (cs == null) || (cs.length() == 0);
    }

    public static boolean isNotEmpty(CharSequence str) {
        return !isEmpty(str);
    }

    public static String toString(Object object) {
        return object == null ? "" : object.toString();
    }

    /**
     * 在String数组的每一项前插入制定值
     *
     * @param arr
     * @param value
     */
    public static String[] StringArrayItemBeforeInsertValue(String[] arr, String value) {
        if (arr != null && arr.length > 0) {
            for (int i = 0; i < arr.length; i++) {
                arr[i] = value + arr[i];
            }
        }
        return arr;
    }

    public static boolean isNumber(String str) {
        return str != null && Pattern.compile("[0-9]*").matcher(str).matches();
    }

    public static boolean startsWith(String str, String[] prefixArr) {
        if (str == null || prefixArr == null)
            return false;
        for (String prefix : prefixArr) {
            if (str.startsWith(prefix))
                return true;
        }
        return false;
    }

    public static String movePointRight2(String doubleText) {
        if (isEmpty(doubleText)) {
            return "";
        }
        return new BigDecimal(doubleText).movePointRight(2).toEngineeringString();
    }

    public static String getRandom32() {
        StringBuilder strRand = new StringBuilder();
        for (int i = 0; i < 32; i++) {
            strRand.append(String.valueOf((int) (Math.random() * 10)));
        }
        return strRand.toString();
    }

    /**
     * 在图片地址前插入制定值后缀前面加入指定值，
     *
     * @param arr
     * @param value
     */
    public static String[] StringArrayItemExtBeforeInsertValue(String[] arr, String value, String extValue) {
        if (arr != null && arr.length > 0) {
            for (int i = 0; i < arr.length; i++) {
                arr[i] = value + imageExtInsertValue(arr[i], extValue);
            }
        }
        return arr;
    }

    public static String getString(String string) {
        return isEmpty(string) ? "" : string;
    }

    /**
     * 在图片地址后缀前面加入指定值，
     *
     * @param imageUrl
     * @param value
     */
    public static String imageExtInsertValue(String imageUrl, String value) {
        if (isNotEmpty(imageUrl)) {
            StringBuilder builder = new StringBuilder(imageUrl);
            builder.insert(imageUrl.lastIndexOf("."), value);
            return builder.toString();
        }
        return imageUrl;
    }

    public static String[] split(String str, String separatorChar) {
        return splitWorker(str, separatorChar, -1, false);
    }

    private static String[] splitWorker(String str, String separatorChars, int max, boolean preserveAllTokens) {
        if (str == null) {
            return null;
        }
        int len = str.length();
        if (len == 0) {
            return new String[0];
        }
        List<String> list = new ArrayList<String>();
        int sizePlus1 = 1;
        int i = 0;
        int start = 0;
        boolean match = false;
        boolean lastMatch = false;
        if (separatorChars == null) {
            while (i < len)
                if (Character.isWhitespace(str.charAt(i))) {
                    if ((match) || (preserveAllTokens)) {
                        lastMatch = true;
                        if (sizePlus1++ == max) {
                            i = len;
                            lastMatch = false;
                        }
                        list.add(str.substring(start, i));
                        match = false;
                    }
                    i++;
                    start = i;
                } else {
                    lastMatch = false;
                    match = true;
                    i++;
                }
        }
        if (separatorChars.length() == 1) {
            char sep = separatorChars.charAt(0);
            while (i < len)
                if (str.charAt(i) == sep) {
                    if ((match) || (preserveAllTokens)) {
                        lastMatch = true;
                        if (sizePlus1++ == max) {
                            i = len;
                            lastMatch = false;
                        }
                        list.add(str.substring(start, i));
                        match = false;
                    }
                    i++;
                    start = i;
                } else {
                    lastMatch = false;
                    match = true;
                    i++;
                }
        } else {
            while (i < len)
                if (separatorChars.indexOf(str.charAt(i)) >= 0) {
                    if ((match) || (preserveAllTokens)) {
                        lastMatch = true;
                        if (sizePlus1++ == max) {
                            i = len;
                            lastMatch = false;
                        }
                        list.add(str.substring(start, i));
                        match = false;
                    }
                    i++;
                    start = i;
                } else {
                    lastMatch = false;
                    match = true;
                    i++;
                }
        }
        if ((match) || ((preserveAllTokens) && (lastMatch))) {
            list.add(str.substring(start, i));
        }
        return (String[]) list.toArray(new String[list.size()]);
    }
}
